package cn.wolfcode.mp.web.controller;

import cn.wolfcode.mp.anno.RequiredPermission;
import cn.wolfcode.mp.domain.Employee;
import cn.wolfcode.mp.query.EmployeeQuery;
import cn.wolfcode.mp.query.JsonResult;
import cn.wolfcode.mp.service.IDepartmentService;
import cn.wolfcode.mp.service.IEmployeeService;
import cn.wolfcode.mp.service.IRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前后端分离项目:
 */
//@RestController
@Controller
//@RequestMapping("emps")
@RequestMapping("employee")
public class EmployeeController {

   /* @Autowired
    private IEmployeeService employeeService;*/

    /*//接口开发
    @GetMapping("/list")
    public Object list(){
        return employeeService.list();
    }

    @GetMapping("/detail")
    public Object detail(Long id){
        return employeeService.getById(id);
    }

    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Employee employee){
        employeeService.saveOrUpdate(employee);
        return JsonResult.success();
    }*/

    @Autowired
    private IEmployeeService employeeService;

   /* @Autowired
    public void setEmployeeService(IEmployeeService employeeService) {
        this.employeeService = employeeService;
    }*/

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    //处理分页过滤查询所有员工请求
    // /employee/list 目前不带参数,响应HTML 格式内容
    @RequiredPermission(name = "员工页面", expression = "employee:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo) {
        //获取所有 员工数据,存到Model 中
        IPage<Employee> page = employeeService.queryPage(qo);
        model.addAttribute("departments", departmentService.list());
        model.addAttribute("page", page);
        return "employee/list";
    }

    // 访问路径 /employee/delete?id=id值
    // 处理查询所有员工删除的请求,浏览器重新发送一条请求去查询一次
    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.removeById(id);
        }
        return "redirect:/employee/list"; //让浏览器再发一条请求去查询
    }

    @RequiredPermission(name = "员工批量删除", expression = "employee:batchDelete")
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        if (ids != null) {
            employeeService.removeById(ids);
        }
        return new JsonResult(true, "删除成功");
    }

    //处理去保存请求或去修改请求 /employee/input 只显示添加页面,可以贴或者不贴
    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {

        // 共享部门
        model.addAttribute("departments", departmentService.list());
        // 共享角色
        model.addAttribute("roles", roleService.list());

        if (id != null) {
            model.addAttribute("employee", employeeService.getById(id));
        }
        return "employee/input";
    }

    //处理保存的请求 /employee/saveOrUpdate 带上参数
    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(Employee employee) {
        if (employee.getId() != null) {
            employeeService.saveOrUpdate(employee);
        }
        return new JsonResult();
    }

    // 查询用户是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String username) {
        HashMap<String, Boolean> map = new HashMap<>();
        Employee employee = employeeService.selectByUsername(username);
        //判断用户名是否为空,如果为空,返回
        map.put("valid", employee == null);
        return map;
    }

    @RequiredPermission(name = "员工导出", expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
      /*  //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）指定下载的文件名
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //生成一个excel,并且返回给浏览器 (文件下载的方式使用)
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建一个表格
        HSSFSheet sheet = wb.createSheet("员工通讯录");
        //创建行
        Row row = sheet.createRow(5);
        //创建单元格
        Cell cell = row.createCell(4);
        //写入内容
        cell.setCellValue("hello");
        //把excel的数据,输出到浏览器 (输出流)
        wb.write(response.getOutputStream());*/

        // 动态获取数据
        // 文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //生成一个excel,并且返回给浏览器 (文件下载的方式使用)
        //HSSFWorkbook wb = employeeService.exportXls();
        //把excel的数据,输出到浏览器 (输出流)
        //wb.write(response.getOutputStream());
    }

    @RequiredPermission(name = "员工导入", expression = "employee:importXls")
    @ResponseBody
    @RequestMapping("/importXls")
    public JsonResult importXls(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //employeeService.importXls(file);
        return new JsonResult();
    }

    // 更改状态
    @ResponseBody
    @RequestMapping("/changeStatus")
    public JsonResult changeStatus(Employee employee) {
        //employeeService.updateStatus(employee);
        return new JsonResult();
    }

    // 查询所有员工
    @ResponseBody
    @RequestMapping("/selectAllEmp")
    public List<Employee> selectAllEmp() {
        return employeeService.list();
    }
}
